package com.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.entity.SysRole;
import com.cw.entity.SysUser;
import com.cw.entity.SysUserRole;
import com.cw.mapper.SysUserRoleMapper;
import com.cw.service.ISysRoleService;
import com.cw.service.ISysUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户角色关系 服务实现类
 * </p>
 *
 * @author smq
 * @since 2021-04-28
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleDao;

    @Autowired
    private ISysRoleService sysRoleService;

    @Override
    public List<SysUserRole> getSysUserRole(String userId) {

        return sysUserRoleDao.getSysUserRole(userId);
    }

    @Override
    public int insertSysUserRole(List<SysUserRole> list) {

        return sysUserRoleDao.insertSysUserRole(list);
    }

    @Override
    public int updateSysUserRole(SysUserRole sysUserRole) {

        return sysUserRoleDao.updateSysUserRole(sysUserRole);
    }

    @Override
    public int deleteSysUserRole(String userId,String roleId) {

        return sysUserRoleDao.deleteSysUserRole(userId, roleId);
    }

    @Override
    public List<String> getRoleNamesByUserId(String userId) {
        return sysUserRoleDao.getRoleNamesByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> getRoleIdAndNamesByUserId(String userId) {
        return sysUserRoleDao.getRoleIdAndNamesByUserId(userId);
    }

    @Override
    public List<String> getUserIdByRoleId(String roleId) {
        return sysUserRoleDao.getUserIdByRoleId(roleId);
    }

    /**
     * 通过角色主键获取这个角色的所有用户
     * @param roleId 角色主键
     * @return java.util.List<com.kszs.entity.SysUser>
     * @author ly
     * @date 2020/10/12 13:59
     */
    @Override
    public List<SysUser> getUserByRoleId(String roleId) {
        return sysUserRoleDao.getUserByRoleId(roleId);
    }

    /**
     * 通过用户主键逻辑删除
     * @param userId 用户主键
     * @return boolean
     * @author ly
     * @date 2021/5/12 11:00
     */
    @Override
    public boolean deleteRoleByUser(String userId) {
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserRole::getUserId, userId);
        lambdaQueryWrapper.eq(SysUserRole::getDeleteFlag, "0");

        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setDeleteFlag("1");

        return update(sysUserRole, lambdaQueryWrapper);
    }

    /**
     * 通过用户主键获取角色
     * @param userId 用户主键
     * @return com.cw.entity.SysRole
     * @author ly
     * @date 2021/5/12 21:14
     */
    @Override
    public SysRole getRoleByUserId(String userId) {
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserRole::getUserId, userId);
        lambdaQueryWrapper.eq(SysUserRole::getDeleteFlag, "0");

        List<SysUserRole> list = list(lambdaQueryWrapper);
        if (CollectionUtils.isEmpty(list)) { return null; }
        SysUserRole sysUserRole = list.get(0);
        return sysRoleService.getById(sysUserRole.getRoleId());
    }
}
